package com.aigou.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aigou.dao.OrderMapper;
import com.aigou.pojo.Order;
import com.aigou.pojo.PageResult;
import com.aigou.pojo.Result;
import com.aigou.service.IOrderService;
import com.aigou.vo.CartVo;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


@RestController
@RequestMapping("/order")
public class OrderController {

	@Autowired
	StringRedisTemplate redisTemplate;
    @Autowired
    IOrderService orderService;
    
    @PostMapping("/add")
    public Result create(@RequestBody Order order) {

		// TODO session获取uid
    	order.setUid(8);
    	orderService.create(order);
    	return new Result();
    }
    
    @PostMapping("/addByCart")
    public Result createByCart(Integer uid) {

		// TODO session获取uid 102
    	String cartStr = redisTemplate.opsForValue().get(String.valueOf(uid));
		if (cartStr == null) {
			
		}
		
		CartVo cartVo = null;
		cartVo = JSON.parseObject(cartStr, CartVo.class);
    	
    	orderService.create(cartVo);
    	return new Result();
    }

    @GetMapping("/findAll")
    public Result findAll(Integer gid, Integer page) {
    	Result r = new Result();
    	QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("gid", gid);
    	queryWrapper.like("gname", "b");
    	
    	IPage<Order> pageObj = new Page<>(page, 3);
		pageObj = orderService.page(pageObj);

		PageResult pr = new PageResult();
    	pr.setRows(pageObj.getTotal());
    	pr.setList(pageObj.getRecords());
    	
		r.setData(pr);
		return r;
    }
    
}
